define(function(require, exports, module) {
    // require('./plug/hammer.min');
    // require('./plug/layer/layer');
    // require('./aes');
    var myPub = require('./public');

    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }

    var m = getQueryString('m');
    if(m){
        $('.js_loginPhoneNum').val(m);
    }



    /******************   账号验证 start        ***********************/
    var oInputBoxList = $('.js_inputBoxList'); //输入款列表
    var oLoginPhoneNum = $('.js_loginPhoneNum'); //手机号码框
    var oGetCode = $('.js_getCode'); //验证码按钮
    var oGetCodeDisabled = $('.js_getCodeDisabled'); //验证码倒计时
    var oClearBtn = $('.js_clearBtn'); //清理按钮
    var oShowPwd = $('.js_showPwd'); //显示和隐藏密码
    var oLoginPassWord = $('.js_loginPassWord'); //密码输入


    var oImgCode = $('.js_imgCode'); //图片验证输入框

    //清除按钮操作
    oClearBtn.hammer().on('tap', function() {
        $(this).parents('.js_loginMob').removeClass('has_error').removeClass('has_success');
    });

    //手机输入框
    oLoginPhoneNum.on('input propertychange', function() {
        phonemobile();
        var _oClearBtn = $(this).siblings('.js_clearBtn');

        $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
    });

    //验证码
    //oGetCode.hammer().on('tap', );
    oGetCode.click(function(){
        var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');
        if(oLoginPhoneNum.val() == '') {
            myPub.TipLayer('请输入手机号！');
        } else if(oLoginMob.hasClass('has_error')) {
            myPub.TipLayer('请输入正确手机号！');
        }
        else if(oImgCode.val() == '') {
            myPub.TipLayer('请输入图片中的验证码！');
        }
        else {
            var typeValue = $('#type').val();
            if(typeValue == 1){
                sendCode({"mobile": oLoginPhoneNum.val(),"type":1, "captch": Encrypt(oImgCode.val())});
            }else{
                sendCode({"mobile": oLoginPhoneNum.val(), "captch": oImgCode.val(), "captch": Encrypt(oImgCode.val())});
            }
        }
    });

    //点击切换图片验证码
    var oGetCodeImg = $('.js_getCodeImg');
    oGetCodeImg.hammer().on('tap',function() {
        var timestamp = (new Date()).valueOf();
        var imageSrc = oGetCodeImg.find('img').attr("src");

        if(imageSrc.indexOf("?") >= 0) {
            imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
        }
        imageSrc = imageSrc + "?timestamp=" + timestamp;
        oGetCodeImg.find('img').attr("src", imageSrc);
    });





    function sendCode(datas) {
        $.ajax({
            url: '/ddjLoginAuth/sendCode',
            data: datas,
            type: 'POST',
            dataType: 'json',
            success: function(data) {
                if(data.code == 'S001') {
                    setCode();
                    myPub.TipLayer(data.msg);
                }
                else if(data.code == 'F_001') {
                    myPub.TipLayer(data.msg);
                }
                else{
                    if(datas['type'] == 1){
                        //注册
                        if(data.code === 'F_003'){
                            //手机已注册
                            myPub.TipBtnLayer(
                                '<div class="layerListInfo"><h4 class="layerList_H4"><p>注册失败!</p><a>'+data.msg+'</a></h4>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goLogin?m='+ datas["mobile"] +'">登录</a></p>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goRetrieve?m='+ datas["mobile"] +'">找回密码</a></div>',
                                'no','取消',function(){myPub.LayerCloseAll();});
                        }
                    }else{
                        if(data.code === 'F_004'){
                            //找回
                            myPub.TipBtnLayer(
                                '<div class="layerListInfo"><h4 class="layerList_H4"><p>找回失败!</p><a>'+data.msg+'</a></h4>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goRegister?m='+ datas["mobile"] +'">注册</a></div>',
                                'no','取消',function(){myPub.LayerCloseAll();});
                        }
                    }
                }

            },
            error:function(x){
                if(403==x.status){
                    window.location.href='/loginExpired';
                }
                console.log(x.status);
            }
        });
    }

    //密码显示和隐藏
    oShowPwd.hammer().on('tap', function() {
        if($(this).hasClass('on')) {
            $(this).removeClass('on').siblings('.js_loginPassWord').attr('type', 'password');
        } else {
            $(this).addClass('on').siblings('.js_loginPassWord').attr('type', 'text');
        }

    });

    //密码输入检测
    oLoginPassWord.each(function(i) {
        oLoginPassWord.eq(i).on('input propertychange', function() {
            userPassWord(oLoginPassWord.eq(i));

            var _oClearBtn = $(this).siblings('.js_clearBtn');
            $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
        });

    });

    /*验证手机号码*/
    function phonemobile() {
        var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');
        myPub.checkMobile(oLoginPhoneNum, function() {
            oLoginMob.removeClass('has_error').removeClass('has_success');
            //myPub.TipLayer('电话号码不能为空');
        }, function() {
            oLoginMob.addClass('has_error').removeClass('has_success');
            //myPub.TipLayer('不是手机号码,请重新谈填写');

        }, function() {
            oLoginMob.addClass('has_success').removeClass('has_error');

        });
    }

    /*验证密码*/
    function userPassWord(obj) {

        var oLoginMob = obj.parents('.js_loginMob');
        myPub.checkPassWord(obj, function() {
            oLoginMob.removeClass('has_error').removeClass('has_success');
        }, function() {
            oLoginMob.addClass('has_error').removeClass('has_success');

        }, function() {
            oLoginMob.addClass('has_success').removeClass('has_error');

        });
    }

    /*倒计时*/
    function setCode() {
        oGetCode.hide();
        oGetCodeDisabled.show();
        myPub.CutTime(60, function() {
            oGetCodeDisabled.hide();
            oGetCode.show().text('重新发送');
        }, function(time) {
            oGetCodeDisabled.text(time + '秒');
        });
    }

    /******************   账号验证 end        ***********************/

        //登录操作
    var oLoginSubmit = $('.js_loginSubmit'); //登录

    oLoginSubmit.hammer().on('tap', function() {

        SublimtBefor(function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('手机号码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('密码不能为空');
                    break;
                case 2:
                    myPub.Tiplayer('验证码不能为空');
            }
        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('请输入正确的手机号码');
                    break;
                case 1:
                    myPub.TipLayer('请输入正确格式的密码');
                    break;
                case 2:
                    myPub.Tiplayer('请输入正确的验证码');
                    break;
            }
        }, function() {
            $.ajax({
                url: '/ddjLoginAuth/login',
                data: {
                    "mobile": $('.js_loginPhoneNum').val(),
                    "password":$('#password').val(),
                    "captch" : Encrypt($('#verifyCode').val())
                },
                type: 'POST',
                dataType: 'json',
                success: function(data) {
                    if(data.code == 'S_000') {
                        myPub.TipLayer(data.msg);
                        var lastUrl = getCookie("historyLastUrl");
                        if(lastUrl !=undefined && lastUrl.length>0){
                            var url = lastUrl.replace(/"/g,'');
                            window.location.href=url;
                        }else{
                            window.location.href="/home";
                        }
                    } else {
                        if(data.code == 'F_004'){
                            //找回
                            myPub.TipBtnLayer(
                                '<div class="layerListInfo"><h4 class="layerList_H4"><p>登入失败!</p><a>'+data.msg+'</a></h4>'+
                                '<p class="layerList_p"><a class="color_href" href="/ddjLoginAuth/goRetrieve?m='+ $('.js_loginPhoneNum').val() +'">找回密码</a></div>',
                                'no','取消',function(){myPub.LayerCloseAll();});
                        }else{
                            myPub.TipLayer(data.msg);
                        }
                    }
                },
                error:function(x){
                    if(403==x.status){
                        window.location.href="/loginExpired";
                    }
                    console.log(x.status);
                }
            });


        })

    });

    //注册下一步操作
    var oNextBtnReg = $('.js_nextBtn_reg');

    oNextBtnReg.hammer().on('tap', function() {

        SublimtBefor(function(index) {
            //console.log(index)
            switch(index) {
                case 0:
                    myPub.TipLayer('手机号码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('请输入右边验证码');
                    break;
                case 2:
                    myPub.TipLayer('验证码不能为空');
                    break;
            }
        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('请输入正确的手机号码');
                    break;
                case 1:
                    myPub.TipLayer('请输入正确格式的验证码');
                    break;
            }

        }, function() {
            $.ajax({
                url: '/ddjLoginAuth/goNextRegister',
                data: {
                    "mobile": $('.js_loginPhoneNum').val(),
                    "smsCode": $('.js_loginCode').val(),
                    "captch" : $('.js_imgCode').val()
                },
                type: 'POST',
                dataType: 'json',
                success: function(data) {
                    if(data.code == 'S_000') {
                        $('#token').val(data.d);
                        $('form[name=goRegisterPwdForm]').submit();
                    } else {
                        myPub.TipLayer(data.msg);
                    }
                },
                error:function(x){
                    if(403==x.status){
                        window.location.href="/loginExpired";
                    }
                    console.log(x.status);
                }
            });
        });

    });

    //找回密码
    var oNextBtnFind = $('.js_nextBtn_update');
    oNextBtnFind.hammer().on('tap', function() {
        SublimtBefor(function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('手机号码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('请输入右边验证码');
                    break;
                case 2:
                    myPub.TipLayer('动态验证码不能为空');
                    break;
            }
        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('请输入正确的手机号码');
                    break;
                case 1:
                    myPub.TipLayer('请输入正确格式的验证码');
                    break;
            }
        }, function() {
            $.ajax({
                url: '/ddjLoginAuth/goUpdatePassword',
                data: {
                    "mobile": $('.js_loginPhoneNum').val(),
                    "smsCode": $('.js_loginCode').val(),
                    "captch" : $('.js_imgCode').val()
                },
                type: 'POST',
                dataType: 'json',
                success: function(data) {
                    if(data.code == 'S_000') {
                        $('#token').val(data.d);
                        $('form[name=goUpdatePasswordForm]').submit();
                    } else {
                        myPub.TipLayer(data.msg);
                    }
                },
                error:function(x){
                    if(403==x.status){
                        window.location.href="/loginExpired";
                    }
                    console.log(x.status);
                }
            });
        });

    });


    /*=======找回密码 start======*/
    //找回下一步操作
    var oNextBtnRetrieve = $('.js_nextBtn_retrieve');
    oNextBtnRetrieve.hammer().on('tap', function() {
        SublimtBefor(function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('手机号码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('请输入右边验证码');
                    break;
                case 2:
                    myPub.TipLayer('动态验证码不能为空');
                    break;
            }
        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('请输入正确的手机号码');
                    break;
                case 1:
                    myPub.TipLayer('请输入正确格式的验证码');
                    break;
            }

        }, function() {
            $.ajax({
                url: '/ddjLoginAuth/goNextRetrieve',
                data: {
                    "mobile": $('.js_loginPhoneNum').val(),
                    "smsCode": $('.js_loginCode').val(),
                    "captch" : $('.js_imgCode').val()
                },
                type: 'POST',
                dataType: 'json',
                success: function(data) {
                    if(data.code == 'S_000') {
                        $('#token').val(data.d);
                        $('#goRetrievePasswordForm').submit();
                    } else {
                        myPub.TipLayer(data.msg);
                    }
                },
                error:function(x){
                    if(403==x.status){
                        window.location.href="/loginExpired";
                    }
                    console.log(x.status);
                }
            });
        });
    });


    //修改、设置密码提交
    var oSetNewPWD = $('.js_setNewPWD'); //新密码
    var oSetNewPWDAgain = $('.js_setNewPWDAgain'); //再次输入新密码

    //注册
    var oLoginSubmit_registerPWD = $('.js_loginSubmit_registerPWD');
    oLoginSubmit_registerPWD.hammer().on('tap', function() {
        SublimtBefor(function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('新密码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('再输入新密码不能为空');
                    break;
            }

        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
                case 1:
                    myPub.TipLayer('再输入密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
            }
        }, function() {
            ContrastPWD(function() {
                $.ajax({
                    url: '/ddjLoginAuth/register',
                    data: $("#registerForm").serialize(),
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S_000') {
                            myPub.TipLayer(data.msg);
                            var lastUrl = getCookie("historyLastUrl");
                            if(lastUrl !=undefined && lastUrl.length>0){
                                var url = lastUrl.replace(/"/g,'');
                                window.location.href=url;
                            }else{
                                window.location.href="/home";
                            }
                        } else {
                            myPub.TipLayer(data.msg);
                        }
                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href="/loginExpired";
                        }
                        console.log(x.status);
                    }
                });
            });
        });
    });

    //找回密码
    var oLoginSubmit_retrievePWD = $('.js_loginSubmit_retrievePWD');
    oLoginSubmit_retrievePWD.hammer().on('tap', function() {
        SublimtBefor(function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('新密码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('再输入新密码不能为空');
                    break;
            }
        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
                case 1:
                    myPub.TipLayer('再输入密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
            }
        }, function() {
            ContrastPWD(function() {
                $.ajax({
                    url: '/ddjLoginAuth/retrievePassword',
                    data: $("#retrievePasswordForm").serialize(),
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S_000') {
                            myPub.TipLayer(data.msg);
                            var lastUrl = getCookie("historyLastUrl");
                            if(lastUrl !=undefined && lastUrl.length>0){
                                var url = lastUrl.replace(/"/g,'');
                                window.location.href=url;
                            }else{
                                window.location.href="/home";
                            }

                        } else {
                            myPub.TipLayer(data.msg);
                        }
                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href="/loginExpired";
                        }
                        console.log(x.status);
                    }
                });
            })
        })
    });

    var loginSubmitUpdatePWD = $('.js_loginSubmit_updatePWD'); //修改密码提交
    loginSubmitUpdatePWD.hammer().on('tap', function() {
        SublimtBefor(function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('旧密码不能为空');
                    break;
                case 1:
                    myPub.TipLayer('新密码不能为空');
                    break;
                case 2:
                    myPub.TipLayer('再输入新密码不能为空');
                    break;
            }

        }, function(index) {
            switch(index) {
                case 0:
                    myPub.TipLayer('旧密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
                case 1:
                    myPub.TipLayer('密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
                case 2:
                    myPub.TipLayer('再输入密码格式错误！请输入6-20位数字和英文字母组合');
                    break;
            }

        }, function() {
            ContrastPWD(function() {
                $.ajax({
                    url: '/ddjLoginAuth/updatePassword',
                    data: $("#updatePasswordForm").serialize(),
                    type: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 'S_000') {
                            myPub.TipLayer(data.msg);
                            var lastUrl = getCookie("historyLastUrl");
                            if(lastUrl !=undefined && lastUrl.length>0){
                                var url = lastUrl.replace(/"/g,'');
                                window.location.href=url;
                            }else{
                                window.location.href="/home";
                            }
                        }else if(data.code == 'FORWARD'){
                            window.location.href=data.d;
                        }
                        else {
                            myPub.TipLayer(data.msg);
                        }
                    },
                    error:function(x){
                        if(403==x.status){
                            window.location.href="/loginExpired";
                        }
                        console.log(x.status);
                    }
                });
            });
        });
    });

    //比较新密码
    function ContrastPWD(fn) {
        if(oSetNewPWDAgain.val() !== oSetNewPWD.val()) {
            myPub.TipLayer('新密码不一致!');
            oSetNewPWDAgain.parents('.js_loginMob').addClass('has_error').removeClass('has_success');
        } else {
            if(fn) fn();
        }
    }

    //提交表单前操作
    //fnEmpty:输入框有未填     fnError:错误格式   fnRight：符合格式
    function SublimtBefor(fnEmpty, fnError, fnRight) {
        var _offBtn = 0;
        var _iNow;

        for(var i = 0; i < oInputBoxList.find('input').length; i++) {
            if(oInputBoxList.find('input.verify').eq(i).val() == '') {
                _offBtn++
                _iNow = i
                break;
            }
        }

        //console.log(_iNow)

        //没有空密码
        if(_offBtn == 0) {
            //错格式密码提示
            if(oInputBoxList.find('.has_error').length > 0) {
                var _errorNum = oInputBoxList.find('.js_login_input').index(oInputBoxList.find('.has_error'))
                //格式不正确
                if(fnError) fnError(_errorNum)
                //myPub.TipLayer('请输入6-20位数字或者英文字母的密码格式');
            } else {
                //没有错误
                if(fnRight) fnRight()
            }

        }
        //有空密码
        else {
            if(fnEmpty) fnEmpty(_iNow);
            //myPub.TipLayer('密码不能为空');
        }

    }

    function getCookie(name)
    {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

});



function Encrypt(word){
    var strInfo = "0102030405060708";
    var key = CryptoJS.enc.Utf8.parse(strInfo);
    var iv  = CryptoJS.enc.Utf8.parse(strInfo);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});
    return encrypted.toString();
}